﻿using System;
using System.Collections.Generic;
using LitJson;
using OestsCommon;
using OestsCommon.Model;
using OestsCommon.TcpNet;
using OestsDataBase;
using OestsServer.Managers;
using OestsServer.Net;
using OSESServer;

namespace OestsServer.Handlers
{
    public class CreateRoomHandler : HandlerBase
    {
        public override PacketKey PKey
        {
            get
            {
                return PacketKey.CreateRoom;
            }
        }

        public override void Execute(ClientChannel client, Request packet)
        {
            Struct3<int, string, string> data = JsonMapper.ToObject<Struct3<int, string, string>>(packet.DataStr);
            int taskId = data.Value1;
            string roomName = data.Value2;
            string roomPassword = data.Value3;
            if (RoomManager.CheckRoomIsExist(roomName))
            {
                ResponsePacket.DataObject = null;
                ResponsePacket.ReturnCode = ReturnCode.Fail;
                ResponsePacket.RetrunMessage = "加入房间失败，房间名已经存在！";
                client.Send(ResponsePacket);
                return;
            }

            TaskModel T = TaskDAL.GetTaskById((int)client.ClientType,taskId);
            Room r = RoomManager.AddRoom(client.ClientType, client.User.DBid, roomName, roomPassword, T);
            ResponsePacket.ReturnCode = ReturnCode.Success;
            ResponsePacket.DataObject = r;
            client.Send(ResponsePacket);  //只向创建者发送消息

            //广播房间列表信息
            BroadcastRoomList(r.Task.Id);
        }

    }
}
